﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Newtonsoft.Json;

namespace RN.ERP.Web.Person
{
    public partial class PersonEmpCertPrintAdd : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GetParams();
                GetEmpQualities();
            }
        }
        public override bool IsCheckPermission
        {
            get { return false; }
        }
        /// <summary>
        /// 取得参数
        /// </summary>
        private void GetParams()
        {
            if (!string.IsNullOrEmpty(Request.QueryString["empGuid"]))
            {
                hdEmpGuid.Value = Request.QueryString["empGuid"].Trim();
            }
            //证书typeflag
            if (!string.IsNullOrEmpty(Request.QueryString["typeflag"]))
            {
                hdTypeFlag.Value = Request.QueryString["typeflag"].Trim();
            }
            //证书编号
            if (!string.IsNullOrEmpty(Request.QueryString["certCode"]))
            {
                hdcertCode.Value = Request.QueryString["certCode"].Trim();
            }
            txtBeginDate.Value = DateTime.Today.ToShortDateString();
        }
        /// <summary>
        /// 取得人员资格证书
        /// </summary>
        private void GetEmpQualities()
        {
            if (string.IsNullOrEmpty(hdEmpGuid.Value))
            {
                return;
            }
            string type = "ZGZS";
            Dictionary<string, string> dict = new Dictionary<string, string>();
            using (PersonService.PersonServiceClient client = new PersonService.PersonServiceClient())
            {
                string code="";
                var list = JsonConvert.DeserializeObject<List<ModelService.Tb_Emp_TitleDetails>>(client.GetEmpQualities(hdEmpGuid.Value, UserInfo.AreaCode));
                var list1 = list.Where(q => q.StampCode == type).ToList();
                foreach (var li in list1)
                {
                    if (li.CertificateCode.Trim() == code)
                    {
                        continue;
                    }
                    if (!dict.ContainsValue(li.CertificateCode.Trim()))
                    {
                        dict.Add(li.CertificateName + "[" + li.CertificateCode + "]", li.CertificateCode.Trim());
                    }
                    code = li.CertificateCode.Trim();
                }
            }
            DropEmpCert.DataSource = dict;
            DropEmpCert.DataTextField = "key";
            DropEmpCert.DataValueField = "value";
            DropEmpCert.DataBind();
            if (DropEmpCert.Items.Count == 0)
            {
                DropEmpCert.Items.Add(new ListItem("暂无证书", ""));
            }
        }
        /// <summary>
        /// 确认打印
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSave_Click(object sender, EventArgs e)
        {
            string url = "PersonEmpCertPrint.aspx";
            if (hdTypeFlag.Value == "4202")
            {
                url = "PersonEmpCertPrintZj.aspx";
            }
            string code = txtCodePrefix.Value.Trim() + txtCode.Value.Trim();
            string certName="";
            string certCode = "";
            if (DropEmpCert.SelectedValue.Length>0)
            {
                certName = DropEmpCert.SelectedItem.Text;
                certName = certName.Split('[')[0];
                certCode = DropEmpCert.SelectedValue.Trim();
            }
            string toUrl = url + "?empGuid=" + hdEmpGuid.Value + "&typeFlag=" + hdTypeFlag.Value + "&certCode=" + hdcertCode.Value + "&beginDate=" + txtBeginDate.Value + "&certCodeNew=" + code + "&certCodeOld=" + certCode + "&certNameOld=" + certName;
            Response.Redirect(toUrl);
        }

    }
}